﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class CMS_Role_RoleMain : System.Web.UI.Page
{
    Usas.BLL.Cms.UsasDB udb = new Usas.BLL.Cms.UsasDB();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                IntiBind();
            }
            catch
            {
            }

        }
    }

    private void IntiBind()
    {
        dptemp.CssClass = "ChileUnHover";
        roleemp.CssClass = "ChileOnHover";
        MainContent.Attributes.Add("src", "GetRoleDepTree.aspx");
        btnAddDpt.Attributes.Add("onclick", "GetDptRole()");
        btnAddRole.Attributes.Add("onclick", "GetRoleRole()");
        btnAddDpt.Visible = true;
        btnAddRole.Visible = false;

        if (Request["rid"].ToString().Length > 0)
        {
            string[] rd = Server.UrlDecode(Request["rid"]).Substring(0, Server.UrlDecode(Request["rid"]).Length - 1).Split(',');
            string[] rn = Server.UrlDecode(Request["ename"]).Substring(0, Server.UrlDecode(Request["ename"]).Length - 1).Split(',');

            for (int i = 0; i < rd.Length; i++)
            {
                lbEmps.Items.Add(new ListItem(rn[i], rd[i]));
            }
        }
        //string dpt = "";
        //string ro = "";
        //string emp = "";
        //string pur = "";
        //for (int i = 0; i < dtt.Rows.Count; i++)
        //{
        //    string p_value = dtt.Rows[i]["p_value"].ToString().Trim().Replace('O', '0');
        //    if (p_value.IndexOf("D") != -1)
        //    {
        //        dpt += p_value.Replace("D", "") + ",";
        //    }
        //    else if (p_value.IndexOf("R") != -1)
        //    {
        //        ro += "'" + p_value + "',";
        //    }
        //    else
        //    {
        //        emp += p_value + ",";
        //    }
        //    pur += p_value + ",";
        //}
        //if (dpt.Length == 0)
        //{
        //    dpt = "1=0";
        //}
        //else
        //{
        //    dpt = " DepartmentId in (" + dpt.Substring(0, dpt.Length - 1) + ")";
        //}

        //if (ro.Length == 0)
        //{
        //    ro = "1=0";
        //}
        //else
        //{
        //    ro = " GROUPID in (" + ro.Substring(0, ro.Length - 1) + ")";
        //}

        //if (emp.Length == 0)
        //{
        //    emp = "1=0";
        //}
        //else
        //{
        //    emp = "EmployeeId in (" + emp.Substring(0, emp.Length - 1) + ")";
        //}
        //DataSet ds = udb.GetRoleDepEmpInfo(dpt, ro, emp);
        //if (ds != null)
        //{
        //    DataTable dt = ds.Tables[0];
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        txtUserListedit.Items.Add(new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i]["id"].ToString()));
        //    }

        //    hidIdedit.Value = pur;
        //}
    }

    protected void roleemp_Click(object sender, EventArgs e)
    {
        dptemp.CssClass = "ChileUnHover";
        roleemp.CssClass = "ChileOnHover";
        MainContent.Attributes.Add("src", "GetRoleDepTree.aspx");
        btnAddDpt.Visible = false;
        btnAddRole.Visible = true;
    }
    protected void dptemp_Click(object sender, EventArgs e)
    {
        dptemp.CssClass = "ChileOnHover";
        roleemp.CssClass = "ChileUnHover";
        MainContent.Attributes.Add("src", "GetEmpDepTree.aspx");
        btnAddDpt.Visible = true;
        btnAddRole.Visible = false;
    }


    protected void btnClear_Click(object sender, EventArgs e)
    {

        for (int i = lbEmps.Items.Count - 1; i >= 0; i--)
        {
            if (this.lbEmps.Items[i].Selected == true)
            {

                this.lbEmps.Items.Remove(this.lbEmps.Items[i]);
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Request["a"].ToString() == "GetUserListEdit")
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "AddUserEdit('" + pass() + "')", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "AddUser('" + pass() + "')", true);
        }

    }

    protected void btnAddDpt_Click(object sender, EventArgs e)
    {
        string[] hid = hidDpt.Value.Split(';');
        for (int i = 0; i < hid.Length; i++)
        {
            string[] ro = hid[i].Split('|');
            string id = ro[0];
            string name = ro[1];
            bool flag = false;
            for (int j = 0; j < lbEmps.Items.Count; j++)
            {
                if (id == lbEmps.Items[j].Value)
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                lbEmps.Items.Add(new ListItem(name, id));
            }
        }
    }
    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        string[] hid = hidRole.Value.Split(';');
        for (int i = 0; i < hid.Length; i++)
        {
            string[] ro = hid[i].Split('|');
            string id = ro[0].TrimStart('0');
            string name = ro[1];
            bool flag = false;
            for (int j = 0; j < lbEmps.Items.Count; j++)
            {
                if (id == lbEmps.Items[j].Value)
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                lbEmps.Items.Add(new ListItem(name, id));
            }
        }
    }

    public string pass()
    {
        string txt = "";
        for (int j = 0; j < this.lbEmps.Items.Count; j++)
        {

            txt += this.lbEmps.Items[j].Text + "?" + lbEmps.Items[j].Value;
            txt += ",";

        }

        if (txt.Length > 0)
        {
            return txt.Substring(0, txt.Length - 1);
        }
        else
        {
            return "";
        }

    }
    protected void USASAll_Click(object sender, EventArgs e)
    {
        lbEmps.Items.Clear();

        lbEmps.Items.Add(new ListItem("Hyperion", "D0"));
    }
}
